Skip to content

lsmt: parallelize appends, serialize resize and restack against in-flight writes - #481

Open
pakkah wants to merge 1 commit into
containerd:mainfrom
pakkah:dev/lsmt-write-concurrency
Open

pakkah wants to merge 1 commit into
containerd:mainfrom
pakkah:dev/lsmt-write-concurrency

Conversation

@pakkah

@pakkah pakkah commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does / why we need it:

  1. LSMT state changes are not safe against concurrent writes. restack, resize, close_seal and Set_Data_File_Direct_IO change the LSMT structure, m_vsize or the append cursor, but none of them was excluded from in-flight writes, and they were not mutually exclusive either. hybrid/sparse writes could land in a sealed layer, index records could disagree with the stack, m_vsize could be read while being bumped, and the direct-IO cursor update could move backwards.
  2. Appends serialize. Every append write held m_rw_mtx, even for disjoint ranges, so throughput dropped as concurrency grew.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Rework concurrency control for LSMT writable layers. Appends no longer serialize, and operations that change the layer stack or its metadata (restack, resize, close_seal) are mutually exclusive with in-flight writes.

  • Replace m_rw_mtx with m_lsmt_rwlock. It protects the structure and metadata of the stacked layer: m_files, m_rw_tag, m_rw_type, m_log, m_vsize, etc. Index contents remain protected by m_index_rwlock, and the index log by its own mutex.
  • Writes that only modify the data file or index without changing the LSMT structure or metadata (pwrite, discard, zero_range, fsync) take the shared read lock and run concurrently. restack, resize and close_seal take the exclusive write lock and wait for in-flight writes.
  • Allocate append positions with m_data_offset.fetch_add. With direct IO, lengths are rounded up to 4K and zero padded, so concurrent appends do not enter the aligned adaptor's read-modify-write path.
  • Add IFileRW::resize and move the current-size read, grow/shrink decision and update_vsize from ImageFile into LSMTFile, all under the write lock. The filesystem resize runs through a lock-free view so resize_extfs does not re-enter the lock through this layer's pwrite.
  • Remove update_vsize from IFileRW; it is now an internal LSMT method.
  • Make m_vsize a hard limit for writes: out-of-range writes return EINVAL. Writers no longer bump m_vsize, so it is only written by resize and restack under the write lock.
  • Take the write lock in close_seal and Set_Data_File_Direct_IO.

Performance

Test Configuration: Append layer, psync I/O engine, 8192 writes per writer, 3 runs, best.

Single vCPU, N photon threads:

Coroutines Before (IOPS) After (IOPS) Gain
1 189,814 180,273 -5%
8 134,499 151,932 +13%
16 102,980 133,935 +30%
32 67,666 112,279 +66%

Multi-vCPU, 1 photon thread per vCPU:

vCPU Before (IOPS) After (IOPS) Gain
1 189,814 180,273 -5%
2 186,548 253,857 +36%
4 168,551 376,486 +123%
8 102,589 248,727 +142%

The baseline serializes every append on m_rw_mtx. Consequently, increasing the number of vCPUs only adds contention and cross-vCPU wakeup costs, causing IOPS to decrease monotonically. After this change, appends can run in parallel, allowing IOPS to scale with the vCPU count (up to 4 vCPUs).

Please check the following list:

  • Does the affected code have corresponding tests, e.g. unit test, E2E test?
  • Does this change require a documentation update?
  • Does this introduce breaking changes that would require an announcement or bumping the major version?
  • Do all new files have an appropriate license header?

@pakkah
pakkah force-pushed the dev/lsmt-write-concurrency branch from dbcc5c1 to 4d56fe7 Compare November 10, 2026 09:39
@pakkah
pakkah marked this pull request as ready for review November 10, 2026 10:12
@liulanzheng
liulanzheng requested a balanced review from Copilot November 11, 2026 11:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Critical bugs remain: restacking can replace a grown device’s live size with an older size, while queued writes can modify a sealed file before hitting a null-index assertion.

5 open issues
What changed in this PR

This PR reworks LSMT concurrency to allow parallel appends while coordinating writes with snapshots, resizing, and sealing.

Changes:

  • Replace append serialization with shared/exclusive locking and atomic append-position allocation.
  • Move resize handling into LSMT, enforce virtual-size write limits, and propagate filesystem-resize link dependencies.
  • Add concurrency and resize tests, reject zero-size image creation, and update snapshot documentation.
File Description
src/​ublk/​ublk_device.cpp Clarifies failed filesystem-grow behavior in a comment.
src/​tools/​overlaybd-create.cpp Rejects zero virtual size.
src/​overlaybd/​lsmt/​test/​test.cpp Adds parallel-write, restack, and filesystem-resize coverage.
src/​overlaybd/​lsmt/​file.h Replaces public update_vsize with resize.
src/​overlaybd/​lsmt/​file.cpp Implements concurrency control, parallel appends, and locked resizing.
src/​overlaybd/​lsmt/​CMakeLists.txt Propagates filesystem-resize dependencies to LSMT consumers.
src/​image_file.cpp Delegates resizing to LSMT.
src/​CMakeLists.txt Removes resize dependencies moved to LSMT.
docs/​live-snapshot.md Documents automatic snapshot/write coordination.
docs/​live-snapshot_zh.md Updates the Chinese snapshot-locking documentation.

Review effort: Balanced

Comment thread src/overlaybd/lsmt/file.cpp Resolved
Comment thread src/overlaybd/lsmt/file.cpp Resolved
Comment thread src/overlaybd/lsmt/test/test.cpp Resolved
Comment thread src/overlaybd/lsmt/test/test.cpp Resolved
Comment thread src/overlaybd/lsmt/test/test.cpp Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Storage locking, append allocation, and resize reentrancy require multi-vCPU runtime validation, which is blocked here by unavailable Photon/e2fsprogs dependencies and the absent extfs callback implementation.

No open issues 🎉

5 resolved since last review

Review effort: Balanced

@liulanzheng

Copy link
Copy Markdown
Member

Why does IOPS decrease monotonically as the number of threads increases? If that is the case, why use multithreading?

@pakkah

pakkah commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Why does IOPS decrease monotonically as the number of threads increases? If that is the case, why use multithreading?

@liulanzheng The benchmark uses psync IO engine with 1 vCPU and N photon threads. A psync syscall blocks the whole vCPU, so adding photon threads does not add IO concurrency, it only adds synchronization overhead. The monotonic decrease in IOPS reflects that overhead.

Multi-vCPU results (psync, 4K writes, 8192 writes per vCPU, 3 runs, best):

vCPU baseline (IOPS) this PR (IOPS) Gain
1 189,814 180,273 -5%
2 186,548 253,857 +36%
4 168,551 376,486 +123%
8 102,589 248,727 +142%

The baseline serializes every append on m_rw_mtx, so adding vCPUs only increases contention and cross-vCPU wakeup cost, and IOPS decreases monotonically. After this change, appends run in parallel, so IOPS scales with vCPU count up to 4 vCPUs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The lock-bypassing resize view depends on unavailable Photon extfs behavior, leaving its I/O contract and concurrent resize, restack, write, and failure paths in need of dependency-aware runtime validation.

No open issues 🎉

Review effort: Balanced

…ight writes

Signed-off-by: huangzhaoxiang.hzx <huangzhaoxiang.hzx@alibaba-inc.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Regression coverage is missing for virtual-size write boundaries across write paths and for concurrent resize/write exclusion, leaving the new limit and race protection unverified.

2 open issues

Review effort: Balanced

Comment on lines +1167 to +1169
if (offset < 0 || (uint64_t)offset > m_vsize || count > m_vsize - (uint64_t)offset)
LOG_ERROR_RETURN(EINVAL, -1, "write beyond virtual size: offset:`, len:`, vsize:`",
offset, count, m_vsize);
int resize(uint64_t target_size, bool resize_fs = false) override {
if ((target_size % ALIGNMENT4K) != 0)
LOG_ERROR_RETURN(EINVAL, -1, "target size must be 4K aligned, got `", target_size);
photon::scoped_rwlock lsmt_lock(m_lsmt_rwlock, photon::WLOCK);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants